#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

vector<vector<int>> ans;
vector<int> track;
bool v[10];

void dfs(int pos, vector<int>& nums){
    if(pos == nums.size()){
        ans.push_back(track);
        return;
    }

    for(int i=0;i<nums.size();i++){
        if(v[i]) continue;
        int last = i;
        while(last + 1 < nums.size()  && nums[last] == nums[last+1]) last++;
        v[i] = 1;
        track.push_back(nums[i]);
        dfs(pos+1, nums);
        v[i] = 0;
        track.pop_back();
        i = last;
    }
    return;
}

vector<vector<int>> permuteUnique(vector<int>& nums) {
    sort(nums.begin(), nums.end());
    dfs(0, nums);
    return ans;
}